package com.abel.amq.consumer.listener;

import com.abel.amq.spring.entity.MailContent;
import com.abel.amq.consumer.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

/**
 * @author abel.he
 * @data 2018/8/8 17:00
 */
@Component(value = "myListener")
public class MyListener implements MessageListener {

    @Autowired
    MessageService messageService;

    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) message;
                Object object = objectMessage.getObject();
                if (object instanceof MailContent) {
                    final MailContent mailContent = (MailContent) object;
                    messageService.sendMail(mailContent);
                }
            } else {
                System.out.println(message);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
